﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FW;

namespace Kcadastro
{
    public partial class visualizar : System.Web.UI.Page
    {
        kcadDataContext db = new kcadDataContext(ConfigurationManager.ConnectionStrings["KcadConnectionString"].ToString());
        private string idemp = "";
        private string nomearq = "";
        private string caminho = "";
        private string nomearquivo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string tipo = Request.QueryString["tp"];
                string i = Request.QueryString["i"];
                carregar(tipo, i);
            }
        }

        private void carregar(string _tp, string _i)
        {
            //string idemp = "";
            FW.cIO io = new FW.cIO();
            switch (_tp)
            {
                case "ERR":

                    Response.Clear();
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"ErrosArquivoOS_{0}\"", _i + ".txt"));
                    Response.ContentType = "text/plain";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    idemp = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).FK_IDemp.ToString();
                    io.NomeArquivo = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp + "/" + _i + "/reportErros.txt";
                    io.LerArquivo();
                    Response.Write(io.Conteudo);

                    break;
                case "XLS":
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"resultado.xls"));
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    idemp = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).FK_IDemp.ToString();
                    nomearq = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp + "/" + _i + "/resultado.xls";
                    //io.LerArquivoSemEnc();
                    Response.WriteFile(nomearq);
                    break;
                case "ZIP":
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"comprovantes.zip"));
                    Response.ContentType = "application/zip";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    idemp = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).FK_IDemp.ToString();
                    nomearq = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp + "/" + _i + "/"+_i+"_comprovantes.zip";
                    //io.LerArquivoSemEnc();
                    Response.WriteFile(nomearq);
                    break;

                case "RET":
                    Response.Clear();
                    caminho = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).caminhoFinal;
                    nomearquivo = Path.GetFileName(caminho);
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}", nomearquivo));
                    Response.ContentType = "application/zip";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    nomearq = caminho;
                    //io.LerArquivoSemEnc();
                    Response.WriteFile(nomearq);
                    break;
                case "CMP":
                    Response.Clear();
                    caminho = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).caminhoComprovantes;
                    nomearquivo = Path.GetFileName(caminho);
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}", nomearquivo));
                    Response.ContentType = "application/zip";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    //idemp = db.tb_os.Single(t => t.PK_IDos == Convert.ToInt64(_i)).FK_IDemp.ToString();
                    nomearq = caminho;
                    //io.LerArquivoSemEnc();
                    Response.WriteFile(nomearq);
                    break;
                case "CUNI":
                    Response.Clear();
                    
                    //Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"",  "resultado.html"));
                    Response.ContentType = "text/html";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    idemp = Session["IDemp"].ToString();
                    io.NomeArquivo = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp + "/consuni/" + _i ;
                    io.LerArquivo();
                    Response.Write(io.Conteudo);
                    break;
                case "CUNIX":
                   
                    Response.Clear();
                    string dataMilitar = DateTime.Now.ToString("yyyyMMdd");
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".xls"));
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    idemp = Session["IDemp"].ToString();
                    nomearq = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp + "/consdiaria/" + dataMilitar + "/" + _i + ".xls";
                    //io.LerArquivo();
                    Response.WriteFile(nomearq);
                    break;
                //case "D9":

                //    Response.Clear();
                //    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".html"));
                //    Response.ContentType = "text/html";
                //    Response.ContentEncoding = System.Text.Encoding.UTF8;
                //    io.NomeArquivo = dbf.tb_danfeConsultaDBIs.Single(t => t.chaveacesso == _i && t.D9 == true).caminho.ToString();
                //    io.LerArquivo();
                //    //lt_arq.Text = io.Conteudo;
                //    Response.Write(io.Conteudo);
                //    break;
                //case "XML":

                //    Response.Clear();
                //    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".xml"));
                //    Response.ContentType = "text/xml";
                //    Response.ContentEncoding = System.Text.Encoding.UTF8;
                //    io.NomeArquivo = dbf.tb_NFEXMLs.Single(t => t.chaveacesso == _i).caminho.ToString();
                //    io.LerArquivo();
                //    //lt_arq.Text = io.Conteudo;
                //    Response.Write(io.Conteudo);
                //    break;
                //case "XMLOP":

                //    Response.Clear();
                //    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".xml"));
                //    Response.ContentType = "text/xml";
                //    Response.ContentEncoding = System.Text.Encoding.UTF8;
                //    io.NomeArquivo = ConfigurationManager.AppSettings["arqPath"] + Session["IDemp"].ToString() + "/XMLOPE_" + _i + ".xml";
                //    io.LerArquivo();
                //    //lt_arq.Text = io.Conteudo;
                //    Response.Write(io.Conteudo);
                //    break;
                //case "DRF":

                //    Response.Clear();
                //    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".html"));
                //    Response.ContentType = "text/html";
                //    Response.ContentEncoding = System.Text.Encoding.UTF8;
                //    io.NomeArquivo = dbf.tb_RFCNPJs.Single(t => t.PKID == Convert.ToInt64(_i)).caminho.ToString();
                //    io.LerArquivo();
                //    //lt_arq.Text = io.Conteudo;
                //    Response.Write(io.Conteudo);
                //    break;
                //case "DSI":

                //    Response.Clear();
                //    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", _i + ".html"));
                //    Response.ContentType = "text/html";
                //    Response.ContentEncoding = System.Text.Encoding.UTF8;
                //    io.NomeArquivo = dbf.tb_SINTEGRAs.Single(t => t.PKID == Convert.ToInt64(_i)).caminho.ToString();
                //    io.LerArquivo();
                //    //lt_arq.Text = io.Conteudo;
                //    Response.Write(io.Conteudo);
                //    break;
            }
            io = null;
            //classes.consDB cons = new classes.consDB();
            //if (_tipo == "xml")
            //{
            //    Response.Clear();
            //    Response.ContentType = "text/xml";
            //    Response.ContentEncoding = System.Text.Encoding.UTF8;
            //}
            //lt_cert.Text = cons.lerPaginaConsulta(_cad, _tipo);
        }
    }
}